package org.jeecg.zhensi.storage.service.impl;

import org.jeecg.zhensi.storage.entity.MaterialStorage;
import org.jeecg.zhensi.storage.entity.MaterialStorageZi;
import org.jeecg.zhensi.storage.mapper.MaterialStorageZiMapper;
import org.jeecg.zhensi.storage.mapper.MaterialStorageMapper;
import org.jeecg.zhensi.storage.service.IMaterialStorageService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 耗材入库
 * @Author: jeecg-boot
 * @Date:   2023-01-13
 * @Version: V1.0
 */
@Service
public class MaterialStorageServiceImpl extends ServiceImpl<MaterialStorageMapper, MaterialStorage> implements IMaterialStorageService {

	@Autowired
	private MaterialStorageMapper materialStorageMapper;
	@Autowired
	private MaterialStorageZiMapper materialStorageZiMapper;
	
	@Override
	@Transactional
	public void saveMain(MaterialStorage materialStorage, List<MaterialStorageZi> materialStorageZiList) {
		materialStorageMapper.insert(materialStorage);
		if(materialStorageZiList!=null && materialStorageZiList.size()>0) {
			for(MaterialStorageZi entity:materialStorageZiList) {
				//外键设置
				entity.setMaterialStorageOrderH(materialStorage.getId());
				materialStorageZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(MaterialStorage materialStorage,List<MaterialStorageZi> materialStorageZiList) {
		materialStorageMapper.updateById(materialStorage);
		
		//1.先删除子表数据
		materialStorageZiMapper.deleteByMainId(materialStorage.getId());
		
		//2.子表数据重新插入
		if(materialStorageZiList!=null && materialStorageZiList.size()>0) {
			for(MaterialStorageZi entity:materialStorageZiList) {
				//外键设置
				entity.setMaterialStorageOrderH(materialStorage.getId());
				materialStorageZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		materialStorageZiMapper.deleteByMainId(id);
		materialStorageMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			materialStorageZiMapper.deleteByMainId(id.toString());
			materialStorageMapper.deleteById(id);
		}
	}
	
}
